/**
 * @Author: Bobby
 * @Date: 2021-11-08 15:20:01
 * @LastEditTime: 2022-01-08 18:58:02
 * @LastEditors: Bobby
 * @Description:
 * @FilePath: \wms-v2\src\vui\plugins\errorLog.js
 * @CIMC-V2
 */
import store from '@commonProject/store'
import { errorLog } from '@project/config'
import { isArray } from '@/utils/validate'

export const needErrorLog = () => {
  const errorLogArray = isArray(errorLog) ? [...errorLog] : [...[errorLog]]
  return errorLogArray.includes(process.env.VUE_APP_ENV)
}

export const addErrorLog = (err) => {
  // eslint-disable-next-line no-console
  if (!err.isRequest) console.error('错误拦截:', err)
  const url = window.location.href
  store.dispatch('errorLog/addErrorLog', { err, url }).then(() => {})
}

export function setup(app) {
  if (needErrorLog()) {
    app.config.errorHandler = addErrorLog
  }
}
